package algorithm.stack;

import java.util.Stack;

/**
 * 设计一个有getMin功能的栈
 * Created by zhy on 2017/9/2.
 */
public class Main {
    private Stack<Integer> stackData;
    private Stack<Integer> stackMin;
    public Main() {
        this.stackData = new Stack<Integer>();
        this.stackMin = new Stack<Integer>();
    }

    public void push(int newNum){
        if(stackMin.isEmpty()){ //如果stackMin为空
            this.stackMin.push(newNum);
        }else if(newNum <= this.getMin()){//或者stackMin不为空,但是newNum<=stackMin的栈顶元素
            this.stackMin.push(newNum);
        }
        this.stackData.push(newNum);//newNum始终要压入stackData中
    }

    public Integer pop(){
        if(this.stackData.isEmpty()){//先判断stackData不为空
            throw new RuntimeException("Your stack is empty.");
        }
        int value = this.stackData.pop();//不为空时，弹出栈顶元素
        if(value == this.getMin()){//当value等于stackMin的栈顶元素时，弹出stackMin的栈顶元素
            this.stackMin.pop();
        }
        return value;
    }

    public Integer getMin(){
        if(this.stackMin.isEmpty()){
            throw new RuntimeException("Your stack is empty.");
        }
        return stackMin.peek();
    }

    public static void main(String[] args) {
        System.out.println();
    }
}
